#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import base64

s = base64.b64encode('在Python中使用BASE 64编码'.encode('utf-8'))
print(s)
d = base64.b64decode(s).decode('utf-8')
print(d)

s = base64.urlsafe_b64encode('在Python中使用BASE 64编码'.encode('utf-8'))
print(s)
d = base64.urlsafe_b64decode(s).decode('utf-8')
print(d)

def safe_base64_decode(f):
    for i in range(len(f)%4):
        if isinstance(f, bytes):
            f=f+b'='
        else:
            f= f+'='
    return base64.b64decode(f).decode('utf-8')
e = safe_base64_decode(s)
#.decode('utf-8')
print(e)